package lihao3;

public class Test5 {
    public static void main(String[] args) {
        TicketSell t1=new TicketSell();
        TicketSell t2 = new TicketSell();
        TicketSell t3 = new TicketSell();
        TicketSell t4 = new TicketSell();
        t1.setName("一号窗口");
        t2.setName("二号窗口");
        t3.setName("三号窗口");
        t4.setName("四号窗口");
        //启动线程
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}

class TicketSell extends Thread {
    private static int ticket = 100;//定义总票数为100张
    private int now = ticket + 1;//定义编号

    public void run() {
        while (ticket > 0) {
            synchronized (TicketSell.class) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(100);//模拟售票需要一定的时间
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(this.getName() + "卖出第 " + (this.now - this.ticket) + " 张票," + " 还剩" + (ticket - 1) + " 张票！");
                    ticket--;
                } else {
                    System.out.println(this.getName() + ":" + "今日票已售完，请另行选择!");
                    break;
                }
            }
        }
    }
}
